<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use app\common\controller\Login;
use think\Request;
use think\Db;
class Shop extends Login
{
	public function index()
	{

		$search = '';
		$data = $this->select($search);
		 
		$page = $data->render();
	 
		$this->assign('page',$page);

		$this->assign('data',$data);

		return $this->fetch();
	}

	public function select($search)
	{

		$agent_id = session::get('agent_id');

		if ($search) {
			$data = Db::table('business_info')
					->Field('i.id as iid,i.sort as isort,i.*,t.*,l.username,c.contact_tel')
					->alias('i')
					->join('shop_type t','i.business_fid = t.id' ,'RIGHT')
					->join('contact c','i.contact_id = c.id' ,'LEFT')
					->join('login l','i.id = l.business_id' ,'RIGHT')
					->where('i.agent_id',$agent_id)
					->where('i.business_name','like','%'.$search.'%')
					->order('i.sort asc,i.id desc')
					->paginate(5);
		}else{

			$data = Db::table('business_info')
					->Field('i.id as iid,i.sort as isort,i.*,t.*,l.username,c.contact_tel')
					->alias('i')
					->join('shop_type t','i.business_fid = t.id' ,'RIGHT')
					->join('contact c','i.contact_id = c.id' ,'LEFT')
					->join('login l','i.id = l.business_id' ,'RIGHT')
					->where('i.agent_id',$agent_id)
					->order('i.sort asc,i.id desc')
					->paginate(5);

		}

		return $data;
	}

	public function search()
	{
		$search = input('post.like');

		if (empty($search)) {
			return $this->redirect('admin/shop/index');
		}
		$data = $this->select($search);
		$page = $data->render();
		//var_dump($data);die;
		$this->assign('page',$page);

		$this->assign('data',$data);

		return $this->fetch('index');

	}
	public function add()
	{
		$data = db('shop_type')->where('agent_id', session::get('agent_id'))->select();
		$this->assign('option',$data);
		return $this->fetch();
	}

	public function do_add()
	{


		$info = $this->do_info();

		$info['business_pt'] = date('Y-m-d');
		$info['agent_id'] = session::get('agent_id');
		$info['business_trial'] = 2;
        $info['sort'] = input('post.sort');
        $info['tuijian'] = (int)input('post.tuijian');
		$dat['username'] = input('post.username');

		$s = db('login')->where('username',$dat['username'])->find();
		if ($s) {
			return $this->error('该登录名称已存在，换一个试试！');die;
		}

		$p = db('info')->where('business_name',$info['business_name'])->find();

		if ($p) {
			return $this->error('该店铺名称已存在，换一个试试！');die;
		}
		$dat['password'] = password_hash(input('post.password'), PASSWORD_DEFAULT);

		$dat['business_id'] = db::name('info')->insertGetId($info);

        db::name('info')->where('id',$dat['business_id'])->update(['business_id'=>$dat['business_id']]);

		$static = db('login')->insertGetId($dat);


		if ($static) {
			return $this->success('添加成功','admin/shop/index');
		}
	}


	public function do_tube()
	{
		$id = input('post.id');
		$info = $this->do_info();
	    $info['agent_id'] = session::get('agent_id');
	    $info['sort'] = (int)input('post.sort');
	    $info['tuijian'] = (int)input('post.tuijian');
	    $info['browsingvolume'] = (int)input('post.browsingvolume');
	    $info['fenxiang'] = (int)input('post.fenxiang');
	    //var_dump($info);die;
		db('info')->where('id',$id)->update($info);

		return $this->success('成功！','admin/shop/index');


	}

	public function delete($id)
	{
		$state = db('info')->where('id' .'='. $id)->delete();
		if ($state) {
			$this->redirect('admin/shop/index');
		}else{
			$this->error('错误','admin/shop/index');
		}
	}


	public function uppwd($id)
	{

		//var_dump($id);die;
		$this->assign('id',$id);

		return $this->fetch();
	}

	public function do_uppwd()
	{
		$data = input('post.');

		if (empty($data['password'])) {
			return $this->error('请输入新密码!');
		}
		$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
		$business_id = $data['business_id'];
		unset($data['business_id']);
		$static = db('login')->where('business_id',$business_id)->update($data);

		if ($static) {
			return $this->success('更改成功!','admin/shop/index');
		}
	}

	public function down($id)
	{

		$data = db('info')->where('id',$id)->find();

		if ($data['business_static']) {
			$arr['business_static'] = 0;
			db('info')->where('id',$id)->update($arr);
			return $this->redirect('admin/shop/index');
		}else{
			$arr['business_static'] = 1;
			db('info')->where('id',$id)->update($arr);

			return $this->redirect('admin/shop/index');
		}


	}

	public function tube($id)
	{
	$data = Db::table('business_info')
					->Field('i.id as iid,i.sort as isort,i.*,t.*')
					->alias('i')
					->join('shop_type t','i.business_fid = t.id' ,'LEFT')
					->where('i.id',$id)
					->find();

		//var_dump($data);die;

		$type = db('shop_type')->where('agent_id', session::get('agent_id'))->select();

		$tuijian = db('info')->find($id);

		$this->assign('tuijian',$tuijian['tuijian']);
		$this->assign('type',$type);

		$this->assign('data',$data);
		return $this->fetch();
	}


	public function do_info()
	{
		$dat['business_name'] = input('post.business_name');
		$dat['business_lng'] = input('post.business_lng');
		$dat['business_lat'] = input('post.business_lat');
		$dat['business_address'] = input('post.business_address');
		$dat['business_fid'] = input('post.business_fid');
		$dat['business_description'] = input('post.business_description');
		$dat['business_announcement'] = input('post.business_announcement');
		$dat['business_action_time'] = input('post.business_action_time');
		$dat['business_last_time'] = input('post.business_last_time');
		$dat['business_tel'] = input('post.business_tel');
		$dat['agent_id'] = input('post.agent_id');
		//var_dump($dat['agent_id']);die;
		if (empty($dat['business_lng'])) {
			return $this->error('请选择店铺坐标哦！');die;
		}
		if (!is_numeric($dat['business_tel'])) {
			return $this->error('联系方式请重新填写！');die;
		}
		$dat['business_logo'] = $this->img();
		if (!$dat['business_logo']) {
			unset($dat['business_logo']);
		}

		//var_dump($dat);die;
		return $dat;
	}



	public function img()
	{
		  $file = request()->file('img');
		   if (!$file) {
		   		return false;
		   }
		    $info = $file->validate(['ext'=>'png,jpg,gif,jpeg'])->move(ROOT_PATH . 'public/uploads/shop');
		    if($info){
		    	  $nowDate=date('Ymd');
		      return  $nowDate.'/'.$info->getFilename();
		    }else{
		       return $this->error('错误!请重新上传图片.');
		    }
	}




	public function trial($id)
	{

		$data = db('info')->where('id',$id)->find();

		if ($data['business_trial'] == 2) {

			$arr['business_trial'] = 1;
			db('info')->where('id',$id)->update($arr);
			return $this->redirect('admin/shop/index');
		}else{

			$arr['business_trial'] = 2;
			db('info')->where('id',$id)->update($arr);

			return $this->redirect('admin/shop/index');
		}


	}

	public function agreement()
	{
		$data = db('settled')->where('id',1)->find();

		$this->assign('data',$data);

		return $this->fetch();
	}

	public function do_agreement()
	{
		$data = input('post.');
		$data['id'] = 1;
		$static = db('settled')->update($data);

		if ($static) {
			return $this->success('修改成功');
		}
	}

	public function set()
	{
		$data = db('settled')->where('id',1)->find();

		$this->assign('data',$data);

		return $this->fetch();
	}

	public function do_set()
	{
		$data = input('post.');

		$static = db('settled')->where('id',1)->update($data);

		return $this->redirect('admin/shop/set');
	}


	public function open($id)
	{

		$d = db('info')->where('id',$id)->where('agent_id',session::get('agent_id'))->find();
		if($d){
			if ($d['business_trial'] !== 2) {
                 exit('未通过审核');
			}
			//var_dump($data);
			$data = db('login')->where('business_id',$d['business_id'])->find();
			if($data){
				session('business_id',$id);
				session('username',$data['username']);
                session('uid',$data['id']);
				session('business_name',$d['business_name']);
				$this->redirect('business/login/action');
			}
		}
		exit('d');
	}

	public function pinglun()
	{


		if (isset($_POST['like'])) {
		$data = db('comment')->where('person_name','like','%'.$_POST['like'].'%')->paginate(10);
		}else{
		$data = db('comment')->paginate(10);
		}
		$page = $data->render();
		//var_dump($data);die;
		$this->assign('page',$page);

		$this->assign('data',$data);

		return $this->fetch();
	}

	public function pinglun_delete($id)
	{
		$static = db('comment')->delete($id);

		if ($static) {
			# code...
			return $this->success('删除成功！');
		}
	}

}
